package com.etlmaster.executor.utils;


import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;


public class StepState implements Cloneable{
	
	private String sessionId;
	
	private String jobId;
	
	private String stackId;
	
	/**
	 * 执行的状态码 0,1,2都返回done
	 * 其他都是返回未完成，不会进入下一个步骤（或等待或异常退出）
	 */
	public static final int 
		RUNNING_SUCCESSED = 0, //运行成功
		RUNNING_FAILED = 1, //执行失败

		NOT_STARTED = -10, //未启动
		ABORTED = -1, //异常中止
		
		RUNNING = 10, //运行中
		SUSPENDED = 11; //挂起
	
	private AtomicInteger status = new AtomicInteger(NOT_STARTED);
	
	private Map<String,Object> returnValue = new HashMap<String,Object>();
	
	/**
	 * 日志参数
	 */
	private Map<String,Object> stateLogParam = new HashMap<String,Object>();
	
	public StepState(String sessionId, String stepId) {
		super();
		this.sessionId = sessionId;
		this.stackId = stepId;
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public String getJobId() {
		return jobId;
	}

	public void setJobId(String jobId) {
		this.jobId = jobId;
	}

	public String getStackId() {
		return stackId;
	}

	public void setStackId(String stepId) {
		this.stackId = stepId;
	}
	
	/**
	 * 步骤是否执行结束，由返回吗来控制下一个步骤
	 * @return
	 */
	public boolean isDone() {
		return status.intValue() == RUNNING_SUCCESSED || status.intValue() == RUNNING_FAILED;
	}
	/**
	 * 运行过了
	 * @return
	 */
	public boolean isRun() {
		return status.intValue() != NOT_STARTED;
	}

	public int getStatus() {
		return status.get();
	}

	public void setStatus(int status) {
		this.status.set(status);
	}

	public Map<String, Object> getReturnValue() {
		return returnValue;
	}

	public void setReturnValue(Map<String, Object> returnValue) {
		this.returnValue.putAll(returnValue);
	}
	
	public void setReturnValue(String key,Object val) {
		this.returnValue.put(key,val);
	}

	public Map<String, Object> getStateLogParam() {
		return stateLogParam;
	}
	public Object clone() throws CloneNotSupportedException {

		StepState metaInstance = (StepState) super.clone();

		return metaInstance;

	}
}
